//
//  GGCalendarCell.m
//  ggtool
//
//  Created by engfei on 2017/5/1.
//  Copyright © 2017年 GaiGai. All rights reserved.
//

#import "GGCalendarCell.h"

#define rowsH 35.0f

@interface GGCalendarHeaderView ()

@property (nonatomic, weak) UILabel *lab;


@end


@implementation GGCalendarHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self setInitialize];
    }
    return self;
}

- (void)setInitialize {
    
    UILabel *lab = [[UILabel alloc] init];
    self.lab = lab;
    lab.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:lab];
    lab.textAlignment = NSTextAlignmentCenter;
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(weakSelf.contentView);
    }];
}

- (void)setOffsetYM:(NSInteger)offsetYM {
    _offsetYM = offsetYM;
    
    NSDate *date = [NSDate date];
    
    NSDateComponents *c = [date YMDComponents];
    c.month = offsetYM;
    c.day = 0;
    c.year = 0;
    NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:c toDate:date options:0];
    
    NSDateComponents *newC = [d YMDComponents];
    self.lab.text = [NSString stringWithFormat:@"%zd年%zd月", newC.year, newC.month];
    
}


@end

@interface GGCalendarCell ()

@property (nonatomic, strong) NSMutableArray *titles;
@property (nonatomic, strong) NSArray *labs;

@end

@implementation GGCalendarCell

- (NSMutableArray *)titles {
    if (_titles == nil) {
        _titles = [NSMutableArray array];
    }
    return _titles;
}

- (NSArray *)labs {
    if (_labs == nil) {
        _labs = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    }
    return _labs;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setInitialize];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}


- (void)setInitialize {

    CGFloat w = kScreenWidth / 7;
    
    for (int i = 0; i < self.labs.count; i++) {
        UILabel *lab = [[UILabel alloc] init];
        [self.contentView addSubview:lab];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.font = [UIFont systemFontOfSize:15];
        lab.text = self.labs[i];
        lab.gg_width = w;
        lab.gg_height = rowsH;
        lab.gg_y = 0;
        lab.gg_x = i * w;
    }
    
    
    
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.contentView addSubview:btn];
            btn.hidden = YES;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:kCommonBlueColor forState:UIControlStateSelected];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
            
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
            [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
            btn.tag = i * 7 + j;
            [btn addTarget:self action:@selector(selectCalendar:) forControlEvents:UIControlEventTouchUpInside];
            [self.titles addObject:btn];
            
            btn.gg_width = w;
            btn.gg_height = rowsH;
            btn.gg_x = j * w;
            btn.gg_y = i * rowsH + rowsH;
            
            
        }
    }
    
}


- (void)setRow:(NSInteger)row {
    _row = row;
    
    for (UIButton *btn in self.titles) {
        btn.hidden = YES;
        btn.enabled = YES;
    }
    
    NSDate *date = [NSDate date];
    // 多少天
    NSUInteger days = [date numberOfDaysInCurrentMonthOffset:row];
    // 当前是几号
     NSUInteger selectedIndex = [date monthlyOrdinality];
    
    NSDateComponents *c = [date YMDComponents];
    c.month = row;
    c.year = 0;
    c.day = 0;
    NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:c toDate:date options:0];

    // 第一天星期几
    NSUInteger firstWeekDay = [[d firstDayOfCurrentMonth] weeklyOrdinality];
        
    for (NSUInteger i = firstWeekDay - 1; i < days + firstWeekDay - 1; i++) {
        UIButton *btn = self.titles[i];
        btn.hidden = NO;
        [btn setTitle:[NSString stringWithFormat:@"%zd", i - (firstWeekDay - 1) + 1] forState:UIControlStateNormal];
        
        if (0 == row) {
            if (i < firstWeekDay + selectedIndex - 1 - 1) {
                btn.enabled = NO;
            } else {
                btn.enabled = YES;
            }
        }
     
    }
}


// 选择日期
- (void)selectCalendar:(UIButton *)sender {
    
    TFLog(@"选择日期 = %zd", sender.tag);
}

- (CGFloat)cellHeight {
    
    NSUInteger weeks = [[NSDate date] numberOfWeeksInCurrentMonthOffset:self.row];

    return weeks * rowsH + rowsH;
}


@end
